﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 公众号素材
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-09-26 
    /// </summary>
    internal class MPMaterialService : DataFactory, MPMaterialIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="type">素材类型</param>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<MPMaterialEntity>> GetList(int type, PageInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<MPMaterialEntity>().And(m => m.MaterialsType == type);
                return await db.GetIQueryable<MPMaterialEntity>().Where(queryWhere).OrderBy(t => t.UploadDate).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 素材信息
        /// </summary>
        /// <param name="mediaId">公众号素材Id</param>
        /// <returns></returns>
        public async Task<MPMaterialEntity> GetInfo(string mediaId)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<MPMaterialEntity>(m => m.MediaId == mediaId);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(MPMaterialEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        public async Task<int> Delete(MPMaterialEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
    }
}
